<?php
class PlusAction extends CommonAction {
	public function index() {
		$Plus = D('Plus');
        $pluspath = "./Lib/Action/Plus/";
        $handle = opendir($pluspath);  //这里输入其它路径
        $dirplus = array();
        while (false !== ($file = readdir($handle))) {
            if (is_file($pluspath.$file) && eregi("Action.class.php$", $file)) {
                if ($file == "CommonAction.class.php") continue;
                $p =  array();
                $dir_name = str_replace("Action.class.php", "", $file);
                $p['dir_name'] = $dir_name;

                $map['mid'] = $this->mid;
                $map['code'] = $dir_name;
                $myplus = $Plus->where($map)->find();

                $$dir_name = A('Plus/'.$dir_name);
                $plus = $$dir_name;

                $p['code'] = $dir_name;
                $p['dir_fullname'] = $file;
                $p['install'] = 0;
                if ($myplus) {
                    $p['install'] = 1;
                    $p['opt'] = unserialize($myplus['opt']);
                }
                if (!$plus->title()) continue;
                $p['title'] = $plus->title();
                $p['desc'] = $plus->desc();
                $p['ctime'] = $plus->ctime();
                $p['ver'] = $plus->ver();
                $dirplus[$p['dir_name']] = $p;
            }
        }
        closedir($handle); 

		$this->assign('volist',$dirplus);
		cookie( '_currentUrl_', __SELF__ );
        $this->assign('mod','Plus');
		$this->display();
		return;
    }

    public function install()
    {
        // $ret = callhttp("http://www.ydapi.com/bbs/plus.php?tid=72");
        $ret = callhttp("http://yunduweixin.duapp.com//index.php?g=Mobile&m=PlusUpdate&a=index&get=json");
        $json = json_decode($ret,true);
        $this->assign('volist',$json);
        $this->display();
    }

    public function uninstall($code='')
    {

    	$map['code'] = $code;
    	$map['mid'] = $this->mid;
    	$Plus = M('Plus');
    	$Plus->where($map)->delete();

        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'uninstall')) {
            R('Plus/'.ucfirst($code).'/uninstall');
        }
        $this->success('插件卸载完成.');
		return;
    }

    public function installplus($code = '',$title = "")
    {
    	$map['code'] = strtolower($code);
    	$map['mid'] = $this->mid;
    	$Plus = M('Plus');
    	$vo = $Plus->where($map)->find();

    	if ($vo) {
    		$this->success('本插件已安装');
    		return;
    	}

        $map['mdk'] = md5($this->mid.time());
        $map['title'] = $title;
    	$Plus->add($map);

        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'install')) {
            R('Plus/'.ucfirst($code).'/install');
        }


    	$this->success('插件已安装完毕');
    }

    public function saveopt($code = null)
    {
        $$k['code'] = A('Plus/'.ucfirst($code));
        $PlusAc = $$k['code'];
        if ($PlusAc) {
            $Plus = M('Plus');
            $map['code'] = strtolower($code);
            $map['mid'] = $this->mid;
            $myplus = $Plus->where($map)->find();
            $tm_myopt = array();
            if ($myplus['opt']) {
                $tm_myopt = unserialize($myplus['opt']);
            }
            $tm_myopt['code'] = $code;
            $myopt = array_merge($tm_myopt,$_POST);
            $data['opt'] = serialize($myopt);
            $Plus->where($map)->save($data);
            $this->success('更新成功');
            return;
        }
        $this->success('更新错误');
    }

    public function delete($code = '')
    {
        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'delete')) {
            $PlusAc->delete();
        }
    }


    public function insert($code = '')
    {
        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'insert')) {
            $PlusAc->insert();
        }
    }

    public function update($code = '')
    {
        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'update')) {
            $PlusAc->update();
        }
    }

    public function edit($code = '',$tpl = 'edit')
    {
        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'edit')) {
            $PlusAc->edit();
        }
        $this->assign('code',$code);
        $this->assign('do',U("Member/Plus/update?code=$code"));
        $this->display('./Lib/Action/Plus/'.ucfirst($code).'/Tpl/'.$tpl.'.html');
    }

    public function add($code = 0,$tpl = 'add')
    {
        $map['code'] = $code;
        $map['mid'] = $this->mid;
        $Plus = M('Plus');
        $plus = $Plus->where($map)->find();
        if (!$plus) {
            $this->error('还没有安装此插件');
            return;
        }
        $vo = unserialize($plus['opt']);
        $this->assign('code',$code);
        $this->assign('plus',$plus);
        $this->assign('do',U("Member/Plus/insert?code=$code"));
        $this->display('./Lib/Action/Plus/'.ucfirst($code).'/Tpl/'.$tpl.'.html');
    }


    public function opt($code = 0,$tpl = 'index')
    {
    	$map['code'] = strtolower($code);
    	$map['mid'] = $this->mid;
    	$Plus = M('Plus');
    	$plus = $Plus->where($map)->find();
    	if (!$plus) {
    		$this->error('还没有安装此插件');
    		return;
    	}
        $vo = unserialize($plus['opt']);
        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, 'index')) {
            $PlusAc->index();
        }
        $this->assign('plus',$plus);
        $this->assign('vo',$vo);
        $this->assign('do',U("Member/Plus/saveopt?code=$code"));
        $this->display('./Lib/Action/Plus/'.ucfirst($code).'/Tpl/'.$tpl.'.html');
        // $this->display('../../Lib/Action/Plus/'.ucfirst($code).'/index.html');
    	// $this->display('../../Lib/Action/Plus/'.ucfirst($code).'/Tpl/index.html');
    }


    public function _empty(){

        $code = $_GET['code'];
        $tpl = I('tpl','index');
        $map['code'] = strtolower($code);
        $map['mid'] = $this->mid;
        $Plus = M('Plus');
        $plus = $Plus->where($map)->find();
        if (!$plus) {
            $this->error('还没有安装此插件');
            return;
        }
        $vo = unserialize($plus['opt']);
        $PlusAc = A('Plus/'.ucfirst($code));
        if (method_exists($PlusAc, $this->ac)) {
            R('Plus/'.ucfirst($code).'/'.$this->ac);
        }
        $this->assign('plus',$plus);
        // print_r($plus);
        // exit();
        $this->assign('vo',$vo);
        $this->assign('do',U("Member/Plus/saveopt?code=$code"));
        $this->display('./Lib/Action/Plus/'.ucfirst($code).'/Tpl/'.$tpl.'.html');
    }

	
}